<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
</div>


<div class="x-body">
    <div class="layui-row">
        <div class="layui-col-md12 x-so">
            <input type="text" name="username" id="borrowPersonNoId" placeholder="请输入借阅人学号" autocomplete="off" class="layui-input">
            <button class="layui-btn" lay-filter="" onclick="searchData();"><i class="layui-icon">&#xe615;</i></button>
        </div>
    </div>
</div>


<table class="layui-hide" id="test" lay-filter="test"></table>
</body>

<script>
    layui.use('table', function () {
        var table = layui.table;
        debugger;
        table.render({
            elem: '#test'
            , url: '/borrow/getBorrowInfo'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , height: 600
            , title: '借阅信息表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', sort: true, fixed: 'left', width: 70}
                , {field: 'borrowPerson', title: '借阅人', width: 80}
                , {field: 'borrowGoods', title: '借阅物品', width: 100}
                , {field: 'borrowNum', title: '借阅数量', width: 100}
                , {
                    field: 'borrowTime', title: '借阅时间',
                    templet: function (d) {
                        return dateToStr(d.borrowTime);
                    }, width: 180
                }
                , {
                    field: 'returnTime', title: '归还时间',
                    templet: function (d) {
                        return dateToStr(d.returnTime);
                    }, width: 180
                }
                , {field: 'borrowHandleState', title: '借阅状态', width: 100}
                , {
                    field: 'createTime', title: '创建时间',
                    templet: function (d) {
                        return dateToStr(d.createTime);
                    }, width: 180
                }
                , {
                    field: 'modifyTime', title: '修改时间',
                    templet: function (d) {
                        return dateToStr(d.modifyTime);
                    }, width: 180
                }
                , {field: 'createPerson', title: '创建人', width: 80}
                , {field: 'modifyPerson', title: '修改人', width: 80}
                , {
                    title: '工具', templet: function (d) {
                        return borrowHandle(d);
                    }
                }
            ]]
            , page: true
        });
        debugger;
        //监听工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === "borrow") {
                $.ajax({
                    method: "post",
                    url: "/borrow/borrowHandleState",
                    data: {borrowId: data.id},
                    success: function (result) {
                        if (0 === result.code) {
                            layer.msg(result.msg, {icon: 1, time: 1000},function () {
                                searchData();
                            });
                        }else{
                            layer.msg(result.msg, {icon: 5, time: 1000});
                        }
                    }
                })
            }
        });
    });

    function dateToStr(date) {
        if (null == date || "" === date) {
            return "";
        }
        var time = new Date(date);
        var y = time.getFullYear();
        var M = time.getMonth() + 1;
        M = M < 10 ? ("0" + M) : M;
        var d = time.getDate();
        d = d < 10 ? ("0" + d) : d;
        var h = time.getHours();
        h = h < 10 ? ("0" + h) : h;
        var m = time.getMinutes();
        m = m < 10 ? ("0" + m) : m;
        var s = time.getSeconds();
        s = s < 10 ? ("0" + s) : s;
        var str = y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
        return str;
    }

    function searchData() {
        debugger;
        var page = $(".layui-laypage-em").next().html();
        layui.table.reload("test", {
            page: {
                curr: page
            },
            where: {
                "borrowPersonNo": $("#borrowPersonNoId").val()
            }
        });
    }
    function borrowHandle(d) {
        var buttonEnable = d.buttonEnable == true ? "borrow" : "";
        var background = d.buttonEnable == true ? "deepskyblue" : "gray";
        var tool = "";
        <shiro:hasPermission name="borrow:borrow">
        tool =
            '<a class="layui-btn layui-btn-danger layui-btn-xs" style="background: ' + background + '" lay-event="' + buttonEnable + '" id="borrowHandleStateId"> <i class="layui-icon"></i>' + d.borrowButtonWord + '</a>'
        </shiro:hasPermission>
        return tool;
    }
</script>
